
//
//  SnapKit+Ex2.swift
//  APP
//
//  Created by LXF on 2018/1/3.
//  Copyright © 2018年 Rdxer. All rights reserved.
//

import UIKit
import SnapKit
import FastComponent
  

//  view.xx.layout xx
public extension XXExtension where Base:UIView {
    
    enum VerticalLayoutAlignment {
        case full
        
        case center(offset:CGFloat = 0,width:CGFloat?)
        case right(offset:CGFloat = 0,width:CGFloat?)
        case left(offset:CGFloat = 0,width:CGFloat?)
    }
    
    enum LayoutFillMode {
        /// 不设限
        case none
        /// 跟父级 边边相等
        case full(offset:CGFloat = 0)
        /// 父级 包裹内容,父级边边可以大于 内容
        case less(offset:CGFloat = 0)
    }
    
    // MARK: -
    // MARK: - Vertical   ↓↓↓↓↓↓↓
    
    /// [EX]垂直方向排布 subviews    ↓↓↓↓↓↓↓
    ///
    /// - Parameters:
    ///   - contentInset: 内部内容的边距, **** 只控制外框
    ///   - item: 每一个item的间距,每一个item的边框
    ///   - itemSpacing: item 之间的间距
    ///   - alignment: 对齐方式, 默认两端对齐,
    ///   - fillMode: 填充模式 默认父类包裹 内容
    func layoutVerticalSubviewsEx(contentInset:UIEdgeInsets = .zero,item:UIEdgeInsets = .zero,itemSpacing: CGFloat = 0,alignment:VerticalLayoutAlignment = .full,fillMode:LayoutFillMode = .less()){
        layoutVerticalEx(views: base.subviews, contentInset: contentInset, item: item,itemSpacing: itemSpacing,alignment: alignment,fillMode: fillMode)
    }
    
    
    /// [EX]垂直方向排布 view    ↓↓↓↓↓↓↓
    ///
    /// - Parameters:
    ///   - views: 需要进行排布的 子 view
    ///   - contentInset: 内部内容的边距, **** 只控制外框
    ///   - item: 每一个item的间距,每一个item的边框
    ///   - itemSpacing: item 之间的间距
    ///   - alignment: 对齐方式, 默认两端对齐,
    ///   - fillMode: 填充模式 默认父类包裹 内容
    func layoutVerticalEx(views:[UIView]? = nil,contentInset:UIEdgeInsets = .zero,item:UIEdgeInsets = .zero,itemSpacing: CGFloat = 0,alignment:VerticalLayoutAlignment = .full,fillMode:LayoutFillMode = .less()){
        
        guard let views = views?.nilOrNotEmpty else {
            XXLoger.e(" 需要布局的 view 为空!!! ")
            return
        }
        
        /// 内容间距 + 子项间距
        let sumInset = contentInset.add(other: item)
    
        let layoutCall = { (make:ConstraintMaker) in
            switch alignment {
                case .full:
                    make.left.equalToSuperview().offset(sumInset.left)
                    make.right.equalToSuperview().offset(sumInset.right)
                case .center(let offset,let  width):
                    make.centerX.equalToSuperview()
                        .offset(sumInset.left + offset)
                    
                    if let width = width{
                        make.width.equalTo(width)
                    }
                    
                case .left(let offset,let  width):
                    make.left.equalToSuperview().offset(sumInset.left + offset)
                    
                    if let width = width{
                        make.width.equalTo(width)
                    }
                case .right(let offset,let  width):
                    make.right.equalToSuperview().offset(sumInset.right + offset)
                    
                    if let width = width{
                        make.width.equalTo(width)
                    }
            }
        }
        
        views.first?.snp.makeConstraints({ (make) in
            make.top.equalToSuperview().offset(sumInset.top)
            
            layoutCall(make)
        })
        
        views.safe_List(from: 1).forEach { (index,v) in
            v.snp.makeConstraints({ (make) in
                
                //  *****   核心
                /// 已经从 1 开始  所以不需要 - 1
                let previouView = views[index]
                
                make.top.equalTo(previouView.snp.bottom)
                    .offset(item.top+item.bottom+itemSpacing)
                layoutCall(make)
            })
        }
        
        views.last?.snp.makeConstraints({ (make) in
            switch fillMode{
                    
                case .none:
                    break
                case .full(let offset):
                    make.bottom.equalToSuperview()
                        .offset(sumInset.bottom + offset)
                case .less(let offset):
                    make.bottom.lessThanOrEqualToSuperview()
                        .offset(sumInset.bottom + offset)
                    
            }
            
        })
        
    }
     
    
}
